<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="机构编号" prop="mechanismCode">
        <el-input
          v-model="queryParams.mechanismCode"
          placeholder="请输入机构编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="机构名称" prop="mechanismName">
        <el-input
          v-model="queryParams.mechanismName"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="testBtn">满杯百香果🍎</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:post:add']"
        >新增
        </el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      row-key="random"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
    >
      <!--      <el-table-column type="selection" width="55" align="center"/>-->

      <el-table-column label="机构编号" align="center" prop="mechanismCode"/>
      <el-table-column label="机构名称" align="center" prop="mechanismName"/>

      <el-table-column label="机构级别" align="center" prop="map">
        <template slot-scope="scope">
          <el-tag
            :key="scope.row.map.label"
            :type="scope.row.map.type"
            effect="plain"
          >
            {{ scope.row.map.label }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="级别名称" align="center" prop="levelName"/>
      <el-table-column label="所在地区" align="center" prop="region"/>
      <el-table-column label="上级机构" align="center" prop="parentMechanismName"/>


      <el-table-column label="操作" align="center" width="300px" class-name="small-padding fixed-width">
        <template slot-scope="scope">


          <!--      =========================================下级机构===========================================================    -->

          <!-- Table -->
          <el-button type="text" icon="el-icon-view" size="mini" @click="underMechanism(scope.row)">下级机构</el-button>

          <el-dialog title="下级机构" width="900px" @close="restPageNum" :visible.sync="dialogTableVisible">

            <el-table
              :data="underMechanismList"
              row-key="random"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
            >
              <el-table-column label="机构编号" align="center" prop="mechanismCode"/>
              <el-table-column label="机构名称" align="center" prop="mechanismName"/>
              <el-table-column label="机构级别" align="center" prop="map">
                <template slot-scope="scope">
                  <el-tag
                    :key="scope.row.map.label"
                    :type="scope.row.map.type"
                    effect="plain"
                  >
                    {{ scope.row.map.label }}
                  </el-tag>
                </template>
              </el-table-column>
              <el-table-column label="机构级别" align="center" prop="levelName"/>
              <el-table-column label="所在地区" align="center" prop="region"/>
              <el-table-column label="上级机构" align="center" prop="parentMechanismName"/>
            </el-table>

            <pagination
              v-show="underTotal>0"
              :total="underTotal"
              :page.sync="queryParams2.pageNum"
              :limit.sync="queryParams2.pageSize"
              @pagination="underMechanism"
            />
          </el-dialog>

          <!--      ====================================================================================================    -->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="empUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          > 员工设置
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:post:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="机构名称" prop="mechanismName">
          <el-input v-model="form.mechanismName" placeholder="请输入机构名称"/>
        </el-form-item>

        <el-form-item label="机构级别" prop="levelId">
          <el-input v-model="form.levelId" placeholder="请输入机构级别"/>
        </el-form-item>

        <el-form-item label="上级机构" prop="parentMechanismName">
          <el-input v-model="form.parentMechanismName" placeholder="请输入上级机构"/>
        </el-form-item>


        <el-form-item label="所在地区" prop="region">
          <v-distpicker :province="form.address.province" :city="form.address.city" :area="form.address.county" @selected="onSelected"></v-distpicker>
        </el-form-item>

        <el-form-item label="电话号码" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入电话号码"/>
        </el-form-item>

        <!--        <el-form-item label="机构编号" prop="postCode">-->
        <!--          <el-input v-model="form.postCode" placeholder="请输入编码名称"/>-->
        <!--        </el-form-item>-->

        <!--        <el-form-item label="登录密码" prop="postCode">-->
        <!--          <el-input v-model="form.postCode" placeholder="请输入编码名称"/>-->
        <!--        </el-form-item>-->

        <!--        <el-form-item label="确认密码" prop="postCode">-->
        <!--          <el-input v-model="form.postCode" placeholder="请输入编码名称"/>-->
        <!--        </el-form-item>-->

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import VDistpicker from 'v-distpicker'
import {test, listMechanism, getUnderMechanism, getListByParam, delPost, addMechanism} from "@/api/zh/mechanism/post";
import log from "../../monitor/job/log";

export default {
  name: "Mechanism",
  dicts: ['sys_normal_disable'],
  components: {VDistpicker},
  data() {
    return {
      // 省市区
      address: {},
      levelColor: [],
      // 当前操作的机构id
      currentMechanismId: -1,
      // 下级机构
      dialogTableVisible: false,
      //添加，修改
      operator: "",  // '0' 添加   '1' 修改
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 下级机构总条数
      underTotal: 0,
      // 机构表格数据
      postList: [],
      // 子机构表格数据
      underMechanismList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mechanismCode: undefined,
        mechanismName: undefined,
        status: undefined
      },
      // 下级机构查询参数
      queryParams2: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined
      },
      // 表单参数
      form: {
        address:{
          province: "",
          city: "",
          county: "",
        }
      },
      // 表单校验
      rules: {
        mechanismName: [
          {required: true, message: "机构名称不能为空", trigger: "blur"}
        ],
        levelId: [
          {required: true, message: "机构级别不能为空", trigger: "blur"}
        ],
        tel: [
          {required: true, message: "电话号码不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 省市区三级联动
    onSelected(data) {
      this.form.address.province = data.province.value
      this.form.address.city = data.city.value
      this.form.address.county = data.area.value
    },
    /** 查询机构列表 */
    getList() {
      this.loading = true;
      listMechanism(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        mechanismCode: undefined,
        mechanismName: undefined,
        levelId: undefined,
        parentMechanismName: undefined,
        status: "0",
        remark: undefined,
        address:{
          province: "",
          city: "",
          county: "",
        },
      };
      this.resetForm("form");
    },
    resetRegion() {
      console.log(this.form)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.loading = true;
      getListByParam(this.queryParams).then(response => {

        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    testBtn() {
      test().then(response => {
        console.log(response);
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.resetRegion();
      this.open = true;
      this.operator = "0";
      this.title = "添加机构";
    },
    /** 下级机构 */
    underMechanism(row) {
      if (row.id != undefined) {
        this.currentMechanismId = row.id;
      }
      this.dialogTableVisible = true;
      this.loading = true;
      getUnderMechanism(this.queryParams2, this.currentMechanismId).then(response => {
        console.log(response);
        this.underMechanismList = response.rows;
        this.underTotal = response.total;
        this.loading = false;
        console.log(this.underMechanismList)
      });
    },
    restPageNum() {
      this.queryParams2.pageNum = 1;
    },
    /** 员工设置 */
    empUpdate(row) {
      this.reset();
      const id = row.id;
      console.log(id);
      // getPost(postId).then(response => {
      //   this.form = response.data;
      this.open = true;
      this.title = "修改岗位";
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      var region = row.region.split(" ");
      this.form.address.province = region[0]
      this.form.address.city = region[1]
      this.form.address.county = region[2]
      this.form.mechanismName = row.mechanismName
      this.form.levelId = row.levelName
      this.form.tel = row.tel
      this.form.parentMechanismName = row.parentMechanismName
      // this.reset();
      this.operator = "1"; // "1":修改
      const id = row.id;

      // getPost(id).then(response => {
      //   this.form = response.data;
      this.open = true;
      this.title = "修改机构";
      // });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.operator == "1") {
            // updatePost(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            //   this.getList();
            // });
          } else {
            console.log(this.form)
            console.log(this.address)
            addMechanism(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const postIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？').then(function () {
        return delPost(postIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
